let questionDetailApp = new Vue({
    el:'#questionDetailApp',
    data:{
        question:{
            title:'测试问题',
            content:'测试问题的正文',
            hits:554,
            userNickName:'擦点',
            gmtCreateText:'12小时前'
        },
        answers:[
            {
                id:1,
                content:'模拟答案-1',
                userNickName: '怪兽1',
                isAccepted:0,
                gmtCreateText:'3分钟前',
                comments:[
                    {userNickName:'评论员5号',content:'模拟评论4'},
                    {userNickName:'评论员4号',content:'模拟评论3'},
                    {userNickName:'评论员3号',content:'模拟评论2'},
                    {userNickName:'评论员2号',content:'模拟评论1'}
                ]
            },{
                id:2,
                content:'模拟答案-2',
                userNickName: '怪兽2',
                isAccepted:0,
                gmtCreateText:'3分钟前',
                comments:[]
            },
            {
                id:3,
                content:'模拟答案-3',
                userNickName: '怪兽3',
                isAccepted:0,
                gmtCreateText:'3分钟前',
                comments:[]
            }
        ]

    },
    methods:{
      loadMyQuestionDetail:function () {
          let id=location.search.substring(1);
          if (id==""||isNaN(id)){
              alert('错误:缺少必要参数');
              location.href='/';
              return;
          }
          $.ajax({
              url:'/portal/questions/'+id,
              success:function (r) {
                  if (r.state==2000){
                      let question = r.data;
                      question.gmtCreateText=getTimeText(question.gmtCreate);

                      questionDetailApp.question = question;
                  }else {
                      alert(r.message);
                      location.href='/';
                  }
              },
          })
      },
        postAnswer:function () {
            // alert("准备发表答案");
            $.ajax({
                url:'/portal/answers/post',
                data:{
                    questionId:location.search.substring(1),
                    content:$('#summernote').val()
                },
                type:'post',
                success:function (r) {
                    if (r.state==2000){
                        alert('发表答案成功!')
                        location.href="#answers"
                        questionDetailApp.loadAnswers();
                        location.href='#answers' ;
                        $('#summernote').summernote('reset');
                    }else {
                        alert(r.message)
                        location.href="/";
                    }
                }
            })
        },
        loadAnswers:function () {
            $.ajax({
                url:'/portal/answers',
                data:'questionId='+location.search.substring(1),
                success:function (r) {
                    let list =r.data;
                    for (let i = 0; i < list.length; i++) {
                        list[i].gmtCreateText=getTimeText(list[i].gmtCreate);
                    }
                    questionDetailApp.answers=list;
                }
            })
        },
        postComment:function (answerId) {
            // alert('准备发表评论');
            $.ajax({
               url:'/portal/comment.post',
                data:{
                    answerId:answerId,
                    content:$('#commentContent'+answerId).val(),
                },
                type: 'post',
                success:function (r) {
                    if (r.state==2000){
                        alert('发表评论成功');

                    }else {
                        alert(r.message);
                        questionDetailApp.loadAnswers();
                    }
                }
            });

        }
    },
    created:function () {
        this.loadMyQuestionDetail();
        //this.loadAnswers();
    }
});
